package com.apes.hr.cultivate.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.*;

/**
 * 培训课程
 * @author Linwz
 * @create 2019-06-17 15:11
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Course.CONST_TABLE_NAME)
public class Course extends Model {
    public static final String CONST_TABLE_NAME = "hr_course";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "培训课程", prefix = "A")
    @Comment("课程编码")
    private String id;   //课程编码

    @Column(name = "name", length = 16)
    @Comment("课程名称")
    private String name;   //课程名称

    @Column(name = "introduce")
    @Comment("课程介绍")
    private String introduce;   //课程介绍

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "course_Methods", referencedColumnName = "id")
    @Comment("授课方式")
    private Enumeration courseMethods; //授课方式

    @Column(name = "value")
    @Comment("课程价值")
    private String value;//课程价值

    @Column(name = "total_hours")
    @Comment("总课时")
    private String totalHours;//总课时

    @Column(name = "state", length = 16)
    @Comment("状态")
    private String state;   //状态

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_id", referencedColumnName = "id")
    private PartyRole deleteId; //删除人

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "delete_date")
    private Date deleteDate;    // 删除日期

    @OneToMany(mappedBy = "course", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<CourseLevel> courseLevelSet = new HashSet(); //评分标准

    public Set<CourseLevel> getCourseLevelSet() {
        for (CourseLevel courseLevel : courseLevelSet) {
            courseLevel.setCourse(this);
        }
        return courseLevelSet;
    }


//    @ManyToMany(cascade = {CascadeType.MERGE},mappedBy = "courses",fetch = FetchType.LAZY)
//    private Set<CultivatePlanItem> cultivatePlanItems;

}
